#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QTextStream>
#include <QLibrary>
#include<QMessageBox>
#include<QtGui/QApplication>
#include<QString>
#include<QVector>
#include<QtAlgorithms>
#include <QTableWidget>
#include <math.h>
#include <QDir>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->lineEdit->setText("ffff");
}


MainWindow::~MainWindow()
{

    delete ui;

}

void MainWindow::on_pushButton_clicked()
{
    ui->lineEdit->setText("12346");

 QLibrary lib("C:\\Users\\LSD\\Desktop\\DIPLOM\\Magistr\\untitled\\debug\\untitled.dll");
 //   QLibrary lib_Vesa("C:\\Users\\LSD\\Desktop\\DIPLOM\\Magistr\\Vesa\\debug\\vesa.dll");
    QLibrary lib_Rast("C:\\Users\\LSD\\Desktop\\DIPLOM\\Magistr\\Rast\\debug\\Rast.dll");
   QLibrary lib_Assoc("C:\\Users\\LSD\\Desktop\\DIPLOM\\Magistr\\assoc\\debug\\assoc.dll");
    QLibrary lib_Weight("C:\\Users\\LSD\\Desktop\\DIPLOM\\Magistr\\weight\\debug\\weight.dll");
    QMessageBox ss;
    if (!lib_Weight.load()) ss.setText(lib_Weight.errorString());
    else ss.setText("OK");
    ss.exec();

        QList<QString> Doc;
        QList<QString>RR;
    Doc.push_back("aaa bbb ccc");
    Doc.push_back("aaa ccc zzz");
    Doc.push_back("bbb ccc aaa");
    Doc.push_back("zzz bbb ccc");
    Doc.push_back("zzz zzz zzz");
    QList<QVector<QString> > Doc1;
    for(int i=0;i<Doc.count();i++)
    {
        RR=Doc.value(i).split(" ");
        Doc1.append(RR.toVector());
        RR.clear();
    }
    typedef void (*Weight)(QList<QVector<QString> >&, QVector<QVector<double> > &, QString &);
    Weight Get_Weight= (Weight) lib_Weight.resolve("Get_Weight");
      if (!Get_Weight) {
          ss.setText("func Get_Weight err");
          ss.exec();
          return;
      }
      ///////////
      typedef void (*Dist)(QList<QString>&);
      Dist Get_Name= (Dist) lib_Rast.resolve("GetDistName");
        if (!Get_Name) {
            ss.setText("func Get_Name err");
            ss.exec();
            return;
        }
        QList<QString> name;
        Get_Name(name);
        for (int i = 0; i < name.length(); ++i)
        {
                 QListWidgetItem *newItem = new QListWidgetItem(tr("%1").arg(name.value(i)));
                 ui->listWidget->addItem(newItem);
             }
      /////////////

    typedef void (*Rast)(QVector<QVector<double> > &, QVector<QVector<double> > &, QString &);
    Rast Get_Rast= (Rast) lib_Rast.resolve("Get_Rast");
      if (!Get_Rast) {
          ss.setText("func Rast err");
          ss.exec();
          return;
      }
      typedef void (*Assoc)(QVector<QVector<double> > &, QVector<QVector<double> > &, QString &);
      Assoc Get_Assoc= (Assoc) lib_Assoc.resolve("Get_Assoc");
        if (!Get_Assoc) {
            ss.setText("func Assoc err");
            ss.exec();
            return;
        }
    ss.setText("func ok");
    ss.exec();
    QString type1="tf",type2="Evclid",type3="Russell and Rao";
   QVector<QVector<double> >list1;
  QVector<QVector<double> >list2;
 //  Get_Vesa(Doc,list1,type1);
 Get_Weight(Doc1,list1,type1);
 // Get_Rast(list1,list2,type2);
 Get_Assoc(list1,list2,type3);



  ui->tableWidget->setColumnCount(Doc.count());
  ui->tableWidget->setRowCount(Doc.count());
  for(int i=0;i<Doc.count();++i)
  {

      for(int j=0;j<Doc.count();++j)
      {
        // ss.setText(QString::number(list2[i].value(j)));
         // ss.exec();
          QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(list2[i].value(j)));
         ui->tableWidget->setItem(i,j,newItem);
      }
  }
}

void MainWindow::on_pushButton_2_clicked()
{
    QList<QString> Doc;
    QList<QString>RR;
Doc.push_back("aaa bbb ccc");
Doc.push_back("aaa ccc zzz");
Doc.push_back("bbb ccc aaa");
Doc.push_back("zzz bbb ccc");
Doc.push_back("zzz zzz zzz");
QList<QVector<QString> > Doc1;
for(int i=0;i<Doc.count();i++)
{
    RR=Doc.value(i).split(" ");
    Doc1.append(RR.toVector());
    RR.clear();
}
QString type1="tf",type2="Evclid",type3="Jaccard";
QVector<QVector<double> >list1;
QVector<QVector<double> >list2;
    QList<QList<QString> >DN;
   QList<QString>type;
      GetDistType(type,DN);
       GetDist(DN,type1,type3,Doc1,list2);
  //   QMessageBox::critical(this,this->windowTitle(),(QString::number(list2.count())));
    ui->tableWidget->setColumnCount(list2.count());
    ui->tableWidget->setRowCount(100);
    for(int i=0;i<list2.count();++i)
    {

        for(int j=0;j<list2[i].count();++j)
    {
        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(list2[i].value(j)));
       ui->tableWidget->setItem(j,i,newItem);
     }
    }
}
void MainWindow::GetDistType( QList<QString> &dll,QList<QList<QString> > &DistName)
{
    QMessageBox ss;

    QList<QList<QString> >DN;
    QDir dir;
     //dir.cd("..\\DIPLOM\\Magistr\\test1-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK_______\\release");
    QString Path=QDir::currentPath();
   //  QMessageBox::critical(this,this->windowTitle(),ss);
    dir.cd(Path);
     QStringList filters;
          filters << "*.dll";
          dir.setNameFilters(filters);
         // QList<QList<QString> > DistName;
     QFileInfoList lst = dir.entryInfoList();
     for (int i = 0; i < lst.size(); ++i)
     {         QFileInfo fileInfo = lst.at(i);
         dll.append(fileInfo.fileName());
         QString FileName=dll.value(i);
        QList<QString> name;
       //  QMessageBox::critical(this,this->windowTitle(),(QString::number(FileName.indexOf("dist_"))));
         if ( FileName.lastIndexOf("dist_")>=0)
         {
           name.push_back(dll.value(i));
            //  QMessageBox::critical(this,this->windowTitle(),"!!!");
             QLibrary lib_Dist( FileName);
             typedef void (*Dist)(QList<QString>&);
             Dist Get_Name= (Dist) lib_Dist.resolve("GetDistName");
               if (!Get_Name)
               {
                   ss.setText("func Get_Name err");
                   ss.exec();
                   return;
               }
               Get_Name(name);
             //  QMessageBox::critical(this,this->windowTitle(),FileName);

               }

             DistName.append(name);
     }
}
void MainWindow::GetDist(QList<QList<QString> >&FN,QString weight_type,QString dist_type,QList<QVector<QString> > &Doc,QVector<QVector<double> >&list)
{
   QVector<QVector<double> >list1;
   QMessageBox ss;
    QLibrary lib_Weight(QDir::currentPath()+"//weight.dll");
    if (!lib_Weight.load()) ss.setText(lib_Weight.errorString());
    else ss.setText("OK");
    ss.exec();
    typedef void (*Weight)(QList<QVector<QString> >&, QVector<QVector<double> > &, QString &);
    Weight Get_Weight= (Weight) lib_Weight.resolve("Get_Weight");
      if (!Get_Weight)
      {
          ss.setText("func Get_Weight err");
          ss.exec();
          return;
      }

      Get_Weight(Doc,list1,weight_type);


     QString fff;

      for(int i=0; i<FN.length()-1;++i)
      {
      int m=0;
      qCount(FN[i],dist_type,m);
      if(m>0)
      {
          fff=(FN[i].value(0));

      }
     }
     //  QMessageBox::critical(this,this->windowTitle(),(fff));
       QLibrary lib_Dist(QDir::currentPath()+"//"+fff);


      typedef void (*Dist)(QVector<QVector<double> > &, QVector<QVector<double> > &, QString &);
      Dist Get_Dist= (Dist) lib_Dist.resolve("Get_Dist");
        if (!Get_Dist) {
            ss.setText("func Get_Dist err");
            ss.exec();
            return;
        }
        Get_Dist(list1,list,dist_type);
}
